package com.itheima.service.impl;

import com.itheima.service.UploadService;
import com.itheima.utils.AliOSSUtils;
import com.itheima.vo.UploadVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

@Service
public class UploadServiceImpl implements UploadService {
    private final AliOSSUtils aliOSSUtils;

    public UploadServiceImpl(AliOSSUtils aliOSSUtils) {
        this.aliOSSUtils = aliOSSUtils;
    }

    @Override
    public UploadVO upload(MultipartFile file) {
        try {
            String str = UUID.randomUUID().toString();
            String fileName = file.getOriginalFilename();
            assert fileName != null;
            String newFileName = "/"+str+"."+fileName.split("\\.")[1];
            File imgFile = new File("D:\\develop\\youdianda\\nginx-youdianda\\html\\backend\\undefined\\"+newFileName);
            file.transferTo(imgFile);
            String contentType = file.getContentType();
            long size = file.getSize();

            UploadVO uploadVO = new UploadVO();
            uploadVO.setSavePath(newFileName);
            uploadVO.setName(fileName);
            uploadVO.setMime(contentType);
            uploadVO.setSize(size);
            return uploadVO;
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败"+e);
        }
    }
}
